﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using DbSharp.Framework.Driver;

namespace DbSharp.Framework.DbObject
{
    class SqlDeclaration : IStatementDeclaration
    {
        #region Private members
        List<StatementArgument> m_ListArgument;
        #endregion

        #region IStatementDeclaration Members

        public string Alias { get; set; }

        public string Remark { get; set; }

        public string ConnectionString{ get; set; }

        public string ConnectionProviderName { get; set; }

        public string CommandText {get ;set ;}

        public CommandType CommandType
        {
            get { return CommandType.Text; }
        } 

        public List<StatementArgument> ListArgument
        {
            get { return m_ListArgument; }
            set { m_ListArgument = value; }
        }

        public StatementArgument FindArgument(string argumentName)
        {
            string upperName = argumentName.Trim().ToUpper();
            upperName = StatementDeclarationExt.NormalizeArgumentName(ConnectionProviderName, ConnectionString, upperName);
            foreach (var arg in m_ListArgument)
            {
                if (arg.Name.ToUpper() == upperName)
                    return arg;
            }
            return null;
        }
        
  

        public DbCommandExecuteType GetCommandExecuteType(ITestDataSource dataSource)
        {
            return StatementDeclarationExt.DetermineDbCmdExecuteType(dataSource, ListArgument);
        }

        public override string ToString()
        {
            return "<" + Alias + ">" + this.CommandText;
        }

        public  IStatementDeclaration CloneStatement()
        {
            SqlDeclaration newSqlObj = new SqlDeclaration();
            newSqlObj.Alias = this.Alias;
            newSqlObj.CommandText = this.CommandText; 
            newSqlObj.ConnectionProviderName = this.ConnectionProviderName;
            newSqlObj.ConnectionString = this.ConnectionString;
            newSqlObj.Remark = this.Remark;
            List<StatementArgument> listArg = new List<StatementArgument>();
            foreach (var arg in this.ListArgument)
            {
                var item = arg.CloneArgument();
                listArg.Add(item);
            }
            newSqlObj.ListArgument = listArg; 
            return newSqlObj; 
        }

        #endregion
    }
}
